using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter25
{
	/// <summary>
	/// Example08: Electric Potential Due to a Uniformly Charged Sphere.
	/// An insulating solid sphere of radius R has a uniform 
	/// positive volume charge density and total charge Q. 
	/// (A) 
	/// Find the electric potential at a point outside the sphere, 
	/// that is for r > R. Take the potential to be zero at r = \infinity.
	/// V_B = k_e Q / r
	/// (B) 
	/// Find the potential at a point inside the sphere, 
	/// that is, for r less than R.
	/// V_D = k_e Q / 2 / R *(3 - r^2/R^2)
	/// </summary>
	public class Example08
	{
		public Example08()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		public void Compute()
		{
			L.Scalar.FunctionOfPosition func = new L.Scalar.FunctionOfPosition(Den);
			L.ChargeDensity rho = new L.ChargeDensity();
            rho.ScalarFunctionOfPosition = func;
			L.Volume v = new L.Volume();
			v.XFrom = -2.0;
			v.XTo = 2.0;
			v.YFrom = -2.0;
			v.YTo = 2.0;
			v.ZFrom = -2.0;
			v.ZTo = 2.0;
			// (A)
			L.Position p = new L.Position();
			p.X = 10.0;
			p.Y = 0.0;
			p.Z = 0.0;
			L.ElectricPotential V = new L.ElectricPotential(rho,v,p);
			result += Convert.ToString(V.V)+"  ";        
			result += Convert.ToString(L.Constant.CoulombConstant*4.0/3.0*Math.PI/p.Norm)+"\r\n";
			// (B)
			p.X = 0.5;
			p.Y = 0.0;
			p.Z = 0.0;
			L.ElectricPotential Vi = new L.ElectricPotential(rho,v,p);
			result += Convert.ToString(Vi.V)+"  ";        
			result += Convert.ToString(L.Constant.CoulombConstant*4.0/3.0*Math.PI/2.0/1.0
				*(3.0-p.Norm*p.Norm/1.0/1.0));
     	}		
		private L.Scalar Den(L.Position r)
		{
            L.Scalar s = new L.Scalar();
			if(r.X*r.X+r.Y*r.Y+r.Z*r.Z < 1.0) s.Magnitude = 1.0;
			else s.Magnitude = 0.0;
            return s;
		}
	}
}
